﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;
using Mang24k99.Model.CLMN;
using Mang24k99.Business.CLMN;
using Castle.Components.Validator;

namespace Mang24k99.Web.CLMN.KtClmnItems
{
    public partial class Edit : System.Web.UI.Page
    {
        protected string RootPath = VirtualPathUtility.ToAbsolute("~/");
        /// <summary>
        /// 分页的名称
        /// </summary>
        string _PageIndexName = "pageIndex";
        /// <summary>
        /// 操作的名称
        /// </summary>
        string _OpName = "op";
        /// <summary>
        /// 主键的名称
        /// </summary>
        string _IDName = "ID";
        string _rblName = "rbl";
        string _layerName = "layer";
        string _parentNoName = "parentNo";
        public int? Layer
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _layerName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _layerName);
            }
        }


        public int? ParentNo
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _parentNoName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _parentNoName);
            }
        }
        public string Rbl
        {
            get
            {
                return ViewStateHelper.GetString(ViewState, _rblName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _rblName);
            }
        }
        string _AddName = "Add";
        string _ModifyName = "Modify";
        /// <summary>
        /// Gets or sets the primary ID.(主键id)
        /// </summary>
        /// <value>The primary ID.</value>
        public int? PrimaryID
        {
            get
            {
                return ViewStateHelper.GetInt(ViewState, _IDName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _IDName);
            }
        }
        /// <summary>
        /// Gets or sets the op.(操作符)
        /// </summary>
        /// <value>The op.</value>
        public string Op
        {
            get
            {
                return ViewStateHelper.GetString(ViewState, _OpName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _OpName);
            }
        }
        /// <summary>
        /// Gets or sets the index of the page.(保存新增或修改是列表页的当前页)
        /// </summary>
        /// <value>The index of the page.</value>
        public int? PageIndex
        {
            get
            {
                return ViewStateHelper.GetInt(ViewState, _PageIndexName);
            }
            set
            {
                ViewStateHelper.SetViewState(ViewState, value, _PageIndexName);
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.(页面载入)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitialParameter();
            }
        }
        /// <summary>
        /// Initials the parameter.(接受必要参数,无参数退出)
        /// </summary>
        private void InitialParameter()
        {

            txtChildNum.Enabled = false;
            txtTrastarNum.Enabled = false;
            Layer = RequestHelper.GetInt(this, _layerName);
            ParentNo = RequestHelper.GetInt(this, _parentNoName);
            Rbl = RequestHelper.GetString(this, _rblName);
            Op = RequestHelper.GetString(this, _OpName);
            PageIndex = RequestHelper.GetInt(this, _PageIndexName);
            PrimaryID = RequestHelper.GetInt(this, _IDName);
            if(!PageIndex.HasValue)
            {
                PageIndex = 1;
            }
            BindData();
        }
        /// <summary>
        /// Binds the data.(绑定基础数据)
        /// </summary>
        private void BindData()
        {
            RunWhen runWhen = RunWhen.Insert;
            if(StringHelper.Compare(Op, _AddName))
            {
                this.lblNav.Text = string.Format(this.lblNav.Text, "新增");
                this.btnSave.Text = "新 增";
                this.lnkbtnAdd.Text = "新增";
                this.txtColumnNO.Text = BKtClmnItems.NewID().ToString();
            }
            else if(StringHelper.Compare(Op, _ModifyName))
            {
                runWhen = RunWhen.Update;
                if(!PrimaryID.HasValue)
                {
                    Response.End();
                }
                this.btnSave.Text = "修 改";
                this.lblNav.Text = string.Format(this.lblNav.Text, "修改");
                this.lnkbtnAdd.Text = "修改";
                BindEdit();
            }
            else
            {
                Response.End();
            }
            WebHelper.SetWebControlAttributes(runWhen, typeof(MKtClmnItems),WebHelper.GetControlsInMaster(this));
        }
        /// <summary>
        /// Binds the edit.(绑定编辑页面)
        /// </summary>
        private void BindEdit()
        {
            MKtClmnItems modelKtClmnItems = BKtClmnItems.Find(PrimaryID);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnNO);
            txtColumnNO.Enabled = false;
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnName);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnAlias);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnLayer);
            WebHelper.SetControlValue(modelKtClmnItems, chkIsReltrastar);
            WebHelper.SetControlValue(modelKtClmnItems, txtChildNum);
            WebHelper.SetControlValue(modelKtClmnItems, ddlIsCreatexml);
            WebHelper.SetControlValue(modelKtClmnItems, txtTrastarNum);
            WebHelper.SetControlValue(modelKtClmnItems, txtTrastarCompos);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnDetailname);
            WebHelper.SetControlValue(modelKtClmnItems, chkIsOrange);
            WebHelper.SetControlValue(modelKtClmnItems, txtOrgnizeNO);
            WebHelper.SetControlValue(modelKtClmnItems, txtChannelNO);
            WebHelper.SetControlValue(modelKtClmnItems, txtMainpageNO);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnNofirst);
            WebHelper.SetControlValue(modelKtClmnItems, txtColmunNosecond);
            WebHelper.SetControlValue(modelKtClmnItems, txtColumnNothird);
            WebHelper.SetControlValue(modelKtClmnItems, ddlColumnAttrib);
            WebHelper.SetControlValue(modelKtClmnItems, ddlIsShow);
            WebHelper.SetControlValue(modelKtClmnItems, ddlIsGrant);
            WebHelper.SetControlValue(modelKtClmnItems, ddlIsMenu);
            WebHelper.SetControlValue(modelKtClmnItems, txtNewsNum);
            WebHelper.SetControlValue(modelKtClmnItems, txtUrlAddress);
        }
        /// <summary>
        /// Handles the Click event of the lnkbtnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void lnkbtnAdd_Click(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// Handles the Click event of the lnkbtnList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void lnkbtnList_Click(object sender, EventArgs e)
        {
            GoServerListUrl(PageIndex);
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string msg = "";
            Control focusControl = null;

            #region 接受操作参数
            bool isAdd = false;
            if(StringHelper.Compare(Op, _AddName))
            {
                isAdd = true;
            }
            else if(StringHelper.Compare(Op, _ModifyName))
            {
                isAdd = false;
            }
            else
            {
                return;
            }
            #endregion
            if(!PageValidate.IsInt(this.txtColumnNO.Text))
            {
                msg += "栏目编号不是有效的int型" + "\r\n";
                MessageBox.SetControlFocusSetControlFocus(this, ref focusControl, this.txtColumnNO);
            }
            if(!PageValidate.IsInt(this.txtColumnLayer.Text))
            {
                msg += "栏目层次不是有效的int型" + "\r\n";
                MessageBox.SetControlFocusSetControlFocus(this, ref focusControl, this.txtColumnLayer);
            }
            if(!PageValidate.IsInt(this.txtChildNum.Text))
            {
                msg += "子栏目数量不是有效的int型" + "\r\n";
                MessageBox.SetControlFocusSetControlFocus(this, ref focusControl, this.txtChildNum);

            }
            if(!PageValidate.IsInt(this.txtTrastarNum.Text))
            {
                msg += "绑定好易星栏目数不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtOrgnizeNO.Text))
            {
                msg += "机构编号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtChannelNO.Text))
            {
                msg += "频道内排序序号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtMainpageNO.Text))
            {
                msg += "首页内排序号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtColumnNofirst.Text))
            {
                msg += "一级栏目编号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtColmunNosecond.Text))
            {
                msg += "二级栏目编号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtColumnNothird.Text))
            {
                msg += "三级栏目编号不是有效的int型" + "\r\n";
            }
            if(!PageValidate.IsIntNullable(this.txtNewsNum.Text))
            {
                msg += "好译星新闻条数不是有效的int型" + "\r\n";
            }

            if(!String.IsNullOrEmpty(msg))
            {
                MessageBox.SetFocus(this, focusControl);
                MessageBox.Show(this, msg);

                return;
            }
            #region 根据操作选项创建实例
            MKtClmnItems modelKtClmnItems;
            if(isAdd)
            {
                modelKtClmnItems = new MKtClmnItems();
            }
            else
            {
                modelKtClmnItems = BKtClmnItems.Find(PrimaryID);
                modelKtClmnItems.LastNum1 = modelKtClmnItems.ColumnNofirst;
                modelKtClmnItems.LastNum2 = modelKtClmnItems.ColmunNosecond;
                modelKtClmnItems.LastNum3 = modelKtClmnItems.ColumnNothird;
                if(modelKtClmnItems == null)
                {
                    return;
                }
            }
            #endregion
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnNO);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnName);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnAlias);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnLayer);
            WebHelper.SetModelValue(modelKtClmnItems, chkIsReltrastar);
            WebHelper.SetModelValue(modelKtClmnItems, txtChildNum);
            WebHelper.SetModelValue(modelKtClmnItems, ddlIsCreatexml);
            WebHelper.SetModelValue(modelKtClmnItems, txtTrastarNum);
            WebHelper.SetModelValue(modelKtClmnItems, txtTrastarCompos);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnDetailname);
            WebHelper.SetModelValue(modelKtClmnItems, chkIsOrange);
            WebHelper.SetModelValue(modelKtClmnItems, txtOrgnizeNO);
            WebHelper.SetModelValue(modelKtClmnItems, txtChannelNO);
            WebHelper.SetModelValue(modelKtClmnItems, txtMainpageNO);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnNofirst);
            WebHelper.SetModelValue(modelKtClmnItems, txtColmunNosecond);
            WebHelper.SetModelValue(modelKtClmnItems, txtColumnNothird);
            WebHelper.SetModelValue(modelKtClmnItems, ddlColumnAttrib);
            WebHelper.SetModelValue(modelKtClmnItems, ddlIsShow);
            WebHelper.SetModelValue(modelKtClmnItems, ddlIsGrant);
            WebHelper.SetModelValue(modelKtClmnItems, ddlIsMenu);
            WebHelper.SetModelValue(modelKtClmnItems, txtNewsNum);
            WebHelper.SetModelValue(modelKtClmnItems, txtUrlAddress);


            msg = "";
            int? parentNo = 0;
            switch(modelKtClmnItems.ColumnLayer)
            {
                case 1:
                    parentNo = 0;
                    modelKtClmnItems.ColumnNofirst = 0;
                    modelKtClmnItems.ColmunNosecond = 0;
                    modelKtClmnItems.ColumnNothird = 0;
                    break;
                case 2:
                    parentNo = modelKtClmnItems.ColumnNofirst;
                    GetMsgFirst(ref msg, parentNo);

                    modelKtClmnItems.ColmunNosecond = 0;
                    modelKtClmnItems.ColumnNothird = 0;
                    break;
                case 3:
                    parentNo = modelKtClmnItems.ColmunNosecond;
                    MKtClmnItems modelSecond = GetMsgSecond(ref msg, parentNo);
                    if(modelSecond == null)
                    {
                        break;
                    }
                    modelKtClmnItems.ColumnNofirst = modelSecond.ColumnNofirst;
                    modelKtClmnItems.ColumnNothird = 0;
                    break;
                case 4:

                    parentNo = modelKtClmnItems.ColumnNothird;
                    MKtClmnItems modelThird = GetMsgThird(ref msg, parentNo);
                    if(modelThird == null)
                    {
                        break;
                    }
                    modelKtClmnItems.ColumnNofirst = modelThird.ColumnNofirst;
                    modelKtClmnItems.ColmunNosecond = modelThird.ColmunNosecond;
                    break;
            }


            if(BKtClmnItems.ExistByName(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnName, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目名称重复\r\n";
            }
            if(BKtClmnItems.ExistByAlias(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnAlias, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目别名重复\r\n";
            }
            if(BKtClmnItems.ExistByDetailname(modelKtClmnItems.ColumnLayer, parentNo, modelKtClmnItems.ColumnDetailname, modelKtClmnItems.ColumnNO))
            {
                msg += "栏目详细名称重复\r\n";
            }

            if(!String.IsNullOrEmpty(msg))
            {
                MessageBox.Show(this, msg);
                return;
            }
            Castle.Components.Validator.RunWhen runWhen = Castle.Components.Validator.RunWhen.Update;
            if(isAdd)
            {
                runWhen = Castle.Components.Validator.RunWhen.Insert;
            }

            bool isSuccess = modelKtClmnItems.IsValid(runWhen);
            if(!isSuccess)
            {

                msg = ActiveRecordValidationBaseHelper.GetErrorMsg(modelKtClmnItems);
                MessageBox.Show(this, msg);


                return;
            }
            if(isAdd)
            {
                msg = "添加成功";
                BKtClmnItems.Create(modelKtClmnItems);
                MessageBox.ShowAndRedirect(this, msg, GetListUrl(0));
            }
            else
            {
                BKtClmnItems.Update(modelKtClmnItems);
                msg = "修改成功";
                MessageBox.ShowAndRedirect(this, msg, GetListUrl(PageIndex));

            }
        }



        private MKtClmnItems GetMsgThird(ref string msg, int? parentNo)
        {
            MKtClmnItems modelFirst = BKtClmnItems.Find(parentNo);
            if(modelFirst == null || modelFirst.ColumnLayer != 3)
            {
                msg += "三级栏目不存在\r\n";
            }
            return modelFirst;

        }

        private MKtClmnItems GetMsgSecond(ref string msg, int? parentNo)
        {
            MKtClmnItems modelFirst = BKtClmnItems.Find(parentNo);
            if(modelFirst == null || modelFirst.ColumnLayer != 2)
            {
                msg += "二级栏目不存在\r\n";
            }
            return modelFirst;
        }

        private static MKtClmnItems GetMsgFirst(ref string msg, int? parentNo)
        {
            MKtClmnItems modelFirst = BKtClmnItems.Find(parentNo);
            if(modelFirst == null || modelFirst.ColumnLayer != 1)
            {
                msg += "一级栏目不存在\r\n";
            }

            return modelFirst;
        }
        /// <summary>
        /// Handles the Click event of the btnReturn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            GoServerListUrl(PageIndex);
        }
        /// <summary>
        /// Goes the server list URL.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        private void GoServerListUrl(int? pageIndex)
        {
            string url = GetListUrl(pageIndex);
            Response.Redirect(url, true);
        }
        /// <summary>
        /// Gets the list URL.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        /// <returns></returns>
        private string GetListUrl(int? pageIndex)
        {
            string op = "?" + "" + _PageIndexName + "=" + pageIndex + "&" + _rblName + "=" + Rbl + "&" + _layerName + "=" + Layer + "&" + _parentNoName + "=" + ParentNo;
            string url = "List.aspx" + op;
            return url;
        }
    }
}
